package com.example.yls.webview;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
//https://www.jianshu.com/p/fd61e8f4049e
public class MainActivity extends AppCompatActivity {
    private WebView my_webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_webview=findViewById(R.id.my_webview);

        //my_webview.loadUrl("http://www.baidu.com");

        //第一种 加载网址
        my_webview.loadUrl("http://ica.gdcp.cn/web/loadWebPage.do?1_649_44015038150_3");

        //第二种 加载assets目录下的本地网页
        //my_webview.loadUrl("file:///android_asset/test.html");

        //第三种 加载html文本内容
        /*String htmlContent="<html>\n"+
                "<title>\n"+
                "本地网页\n"+
                "</title>\n"+
                "<body>\n"+
                "hello world<br>\n"+
                "<img src=\"file:///android_asset/a.png\">\n"+
                "</body>\n"+
                "</html>";
        //my_webview.loadData(htmlContent,"text/html", "utf-8");
        //加载出图片
        my_webview.loadDataWithBaseURL(null,htmlContent,"text/html",
                "utf-8",null);*/

        //第四种 加载SD卡上的html(先要在AndroidManifest.xml上写“READ_EXTERNAL_STORAGE 读取权限”）
        //my_webview.loadUrl("file:///mnt/shared/Other/test.html");

        //问题1 跳转的时候打开了系统浏览器
        my_webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("MainActivity","URL="+url);
                view.loadUrl(url);
                return true;
            }

            //加载开始
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("MainActivity","onPageStarted");
                super.onPageStarted(view, url, favicon);
            }

            //加载完成
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e("MainActivity","onPageFinished");
                super.onPageFinished(view, url);
            }
        });

        //前进
        findViewById(R.id.go_foward).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(my_webview.canGoForward()){
                    Log.e("MainActivity","GoForward");
                    my_webview.goForward();
                }
            }
        });

        WebSettings webSettings = my_webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true); //缩放
        webSettings.setDisplayZoomControls(true); //缩放按钮
        webSettings.setBuiltInZoomControls(true);
    }

    //问题2 点击返回键退出程序
    @Override
    public void onBackPressed() {
        Log.e("MainActivity","onBackPressed");
        if(my_webview.canGoBack()){
            my_webview.goBack();
        }else {
            super.onBackPressed();
        }
    }
}
